#!/bin/bash
#pMusic - external menu
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
SF='space-expand="false" space-fill="false"'
ST='space-expand="true" space-fill="true"'

case $PTR_BTN in
	1)
		SEARCH_COLLECTIONS=true
		SEARCH_MYMUSIC=true
		SEARCH_ALBUMART=false
		SEARCH_YOUTUBE=false
		SEARCH_JAMENDO=false
		SEARCH_RADIO=false
		SEARCH_PODCAST=false
		. $APPDIR/func -search_refresh no_albumart
		read SOURCE < $WORKDIR/filebrowser_dir
		$APPDIR/func -add_to_history "$SOURCE" &
		;;
	2)
		EXIT=view
		;;
	3)
		template(){
			echo '
			<eventbox above-child="true" visible-window="true">
			 <hbox spacing="7" border-width="3">
			  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
			  <text xalign="0" '$TOOLTIP' '$ST'>
			   '$SENSITIVE'
			   <label>'$1'</label>
			  </text>
			 </hbox>
			 <action signal="enter-notify-event">hide:'$2'</action>
			 <action signal="enter-notify-event">show:'$2'B</action>
			 <variable>'$2'</variable>
			</eventbox>
			<eventbox name="rightclick_menu" above-child="true" visible-window="true" visible="false">
			 <hbox spacing="7" border-width="3">
			  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
			  <text xalign="0" '$TOOLTIP' '$ST'>
			   '$SENSITIVE'
			   <label>'$1'</label>
			  </text>
			 </hbox>
			 <action signal="leave-notify-event">show:'$2'</action>
			 <action signal="leave-notify-event">hide:'$2'B</action>
			 <action signal="button-release-event">EXIT:'$2'</action>
			 <variable>'$2'B</variable>
			</eventbox>'
		}

		[ "$1" = disabled ] && SENSITIVE='<sensitive>false</sensitive>'
		export gtkdialog_menu='
		<window decorated="false" border-width="0" skip_taskbar_hint="true" resizable="false" modal="true" '$ST'>
		 <eventbox name="rightclick_menu_frame" above-child="false">
		  <vbox border-width="1">
		   <eventbox>
		    <vbox spacing="0" border-width="7">

		     '"
		     $(template " $(gettext 'Add album to playqueue')" add icon_add_playqueue.svg)
		     $(template " $(gettext 'Search content (left-click)')" search icon_find.svg)
		     $(template " $(gettext 'View album-art (middle-click)')" view icon_select-color.svg)
		     $(template " $(gettext 'Edit in image-editor')" edit icon_edit.svg)
		     $(template " $(gettext 'Delete source-file')" delete icon_delete.svg)
		     "'

		     <hseparator height-request="5"></hseparator>
		     
		     '"
		     $(template " $(gettext 'Larger thumbs')" larger_thumbs)
		     $(template " $(gettext 'Smaller thumbs')" smaller_thumbs)
		     $(template " $(gettext 'Update sizing')" update_size icon_apply.svg)
		     "'

		    </vbox>
		   </eventbox>
		  </vbox>
		 </eventbox>
		 <action signal="focus-out-event">EXIT:exit</action>
		</window>'

		I=$IFS; IFS=""
		for STATEMENTS in $($GTKDIALOG -p gtkdialog_menu -G +"$(($PTR_X_ROOT-5))"+"$(($PTR_Y_ROOT-5))"); do
			eval $STATEMENTS
		done
		IFS=$I
		;;
esac

export PTR_BTN=""
read DIR_BROWSER < $WORKDIR/filebrowser_dir
SOURCE="$STORAGE_DIR/albumart/${DIR_BROWSER}.jpg"
case "$EXIT" in
	search)
		$APPDIR/func -search_refresh no_albumart &
		;;
	add)
		read < $WORKDIR/filebrowser_dir
		TMP="`LC_ALL=C grep -Fim1 "${REPLY}.pmu|" "$STORAGE_DIR/index_mymusic" | cut -d'|' -f14`" #grep -i is helpful
		[ "$TMP" ] && $APPDIR/func_add -add "$TMP"
		;;
	view)
		if [ "$SOURCE" ] && [ "$SOURCE" != "$(gettext 'Search for music')"  ]; then
			echo '
			<window title="'${DIR_BROWSER}'" window_position="2" icon-name="pmusic">
			 <eventbox above-child="true" visible-window="true" spacing="0" '$SF'>
			  <pixmap><input file>"'$SOURCE'"</input></pixmap>
			  <action signal="button-release-event">EXIT:exit</action>
			 </eventbox>
			</window>' | $GTKDIALOG -s &
		fi
		;;
	edit)
		export GTK2_RC_FILES="$HOME/.gtkrc-2.0"
		$IMAGE_EDITOR "$SOURCE" &
		;;
	delete)
		FRAME="$(gettext 'Delete source file')"; IMG="delete"; TXT1="$SOURCE"
		. $APPDIR/box_yesno
		if [ $EXIT = "yes" ]; then
			rm "$SOURCE"
			$APPDIR/func -search_albumart_update
		fi
		;;
	larger_thumbs)
		
		export ALBUMART_SEARCH_HEIGHT=$(($ALBUMART_SEARCH_HEIGHT+20))
		. $APPDIR/func_config -write_config
		rm -f $WORKDIR/xml_pmusic* #remove xml-files to force fresh load of all guis (langauge change) 
		pmusic #new start to load changed settings
		;;
	smaller_thumbs)
		export ALBUMART_SEARCH_HEIGHT=$(($ALBUMART_SEARCH_HEIGHT-20))
		[ $ALBUMART_SEARCH_HEIGHT -le 20 ] && export ALBUMART_SEARCH_HEIGHT=20
		. $APPDIR/func_config -write_config
		rm -f $WORKDIR/xml_pmusic*
		pmusic
		;;
	update_size)
		$APPDIR/func_config -write_config
		rm -f $WORKDIR/xml_pmusic*
		pmusic
		;;
esac
exit 0
